﻿using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;

namespace DjBlog.Rss
{
    /// <summary>
    ///  取得聚合文章
    /// </summary>
    public partial class feed : System.Web.UI.Page
    {

        protected IList<DjModel.PostInfo> postlist = null;
        private void Page_Load(object sender, System.EventArgs e)
        {
            Response.ContentType = "application/xml"; // 输出并按xml数据显示
            Response.Write(GetRSS());
        }

        /// <summary>
        /// 取得聚合文章
        /// </summary>
        public string GetRSS()
        {
            postlist = DjBll.Post.GetPostList(1, 50, null);
            StringBuilder sb = new StringBuilder();
            sb.Append("<rss version=\"2.0\"><channel><title>阿拉神丁的博客</title><link>http://www.alashending.com/blog</link><description>阿拉神丁的博客，专注前端开发。</description>");
            if (postlist != null && postlist.Count > 0)
            {
                foreach (DjModel.PostInfo post in postlist)
                {
                    sb.Append("<item>");
                    sb.AppendFormat("<title><![CDATA[{0}]]></title>",post.Title);
                    sb.AppendFormat("<link>http://www.alashending.com/post/{0}.html</link>",post.ID);
                    sb.AppendFormat("<description><![CDATA[{0}]]></description>", HttpContext.Current.Server.HtmlDecode(post.Content));
                    sb.AppendFormat("<copyright>阿拉神丁</copyright>");
                    string[] s = post.CatString.Split(',');
                    for (int j = 0; j < s.Length; j++)
                    {
                        int cate = 0;
                        if(int.TryParse(s[j],out cate))
                        {
                            if(DjBll.Term.TermCateDic.ContainsKey(cate))
                            {
                                sb.AppendFormat("<category domain=\"http://www.alashending.com/cat/{1}.html\">{0}</category>",
                                    DjBll.Term.TermCateDic[cate].TermName, cate);
                            }
                        }
                    }
                    
                    sb.AppendFormat("<pubDate>{0}</pubDate>",post.CreateDate.ToString("yyyy-MM-dd HH:mm:ss"));
                    sb.AppendFormat("<comments>http://www.alashending.com/post/{0}.html#comment</comments>", post.ID);
                    sb.Append("</item>");
                }
            }
            sb.Append("</channel></rss>");

            return sb.ToString();
        }
    }
}
